<template>
  <el-card class="account-container">
    <h1 :style="`font-size: var(--el-font-size-extra-large)`" style="font-size: 26px; color: #615f5f">类别管理</h1>
    <el-divider />
    <el-row :gutter="12">
      <el-col :span="12">
        <el-card shadow="hover">
          <el-table :data="tableData" style="width: 100%" max-height="250">
            <el-table-column prop="id" label="ID" width="100" />
            <el-table-column prop="name" label="类别名称" width="200"/>
            <el-table-column prop="typeId" label="父类别索引" width="200"/>
            <el-table-column fixed="right" label="操作" width="170">
              <template #default="scope">
                <el-button
                    link
                    type="primary"
                    size="large"
                    @click.prevent="detail(scope.$index)"
                >
                  修改
                </el-button>
                <el-button
                    link
                    type="primary"
                    size="large"
                    @click.prevent="deleteRow(scope.$index)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card shadow="hover">
          <h1 :style="`font-size: var(--el-font-size-extra-large)`" style="font-size: 18px; color: #615f5f">添加类别</h1>
          <el-form
              :label-position="'right'"
              label-width="100px"
              :model="addForm"
              style="max-width: 460px"
          >
            <el-form-item label="类型名称">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="描述">
              <el-input v-model="addForm.information" />
            </el-form-item>
            <el-form-item label="对应的父类别" :label-width="formLabelWidth">
              <el-select v-model="addForm.typeId" clearable placeholder="Select">
                <el-option
                    v-for="item in types"
                    :key="item.dishTypeId"
                    :label="item.typeName"
                    :value="item.dishTypeId"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth">
              <el-button type="primary" @click="onAddItem">提交</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>

    <el-divider />
<!--   消息对话框   -->



    <el-dialog v-model="dialogFormVisible" title="修改类别">
      <el-form :model="form">
        <el-form-item label="父类别" :label-width="formLabelWidth">
          <el-select v-model="form.typeId" clearable placeholder="Select">
            <el-option
                v-for="item in types"
                :key="item.dishTypeId"
                :label="item.typeName"
                :value="item.dishTypeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="类别名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmClickChange"
                   :disabled= "form.typeId==='' || form.name===''">
          确认修改
        </el-button>
      </span>
      </template>
    </el-dialog>



  </el-card>
</template>

<script setup>

import {onMounted, ref} from "vue";
import {addType, deleteType, listCategory, listType, updateType} from "@/api/type";
import {ElMessage} from "element-plus";
import {open_error, open_success} from "@/utils/common";

const tableData = ref([]);

const types = ref([]);

const dialogFormVisible = ref(false);

const form = ref({

});

const addForm = ref({

});



const open2 = () => {
  ElMessage({
    showClose: true,
    message: '成功',
    type: 'success',
  })
}
const open3 = () => {
  ElMessage({
    showClose: true,
    message: 'Warning, this is a warning message.',
    type: 'warning',
  })
}
const open4 = (message) => {
  ElMessage({
    showClose: true,
    message: message,
    type: 'error',
  })
}



const confirmClickChange = () => {
  updateType(form.value).then((res) => {
    if(res.state === 0){
      open_success("成功")
    } else {
      open_error(res.message);
    }
    dialogFormVisible.value = false;
  })
}


const detail = (index) => {
  form.value = tableData.value[index];
  dialogFormVisible.value = true;
}



const onAddItem  = () => {
  addType(addForm.value).then((res) => {
    if(res.state !== 0) {
      open4(res.message);
    } else {
      open2();
    }
    list();
  });
}

const deleteRow = (index) => {
  console.log(tableData.value[index].id);
  const form = ref({
    id: tableData.value[index].id,
  })
  deleteType(form.value).then((res) => {
    console.log(res);
    list();
  })
}

const list = () => {
  listType().then((res) => {
    types.value = res.data;
  });
  listCategory().then((res) => {
    console.log(res.data);
    tableData.value = res.data;
  });
}

onMounted(() => {
  list();
});

</script>
<style scoped>
.course {
  font-weight: bold;
  display: inline-block;
}
.course li {
  line-height: 36px;
  color: #409eff;
  cursor: pointer;
}
</style>